package com.qixin.api.serviceimpl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.api.bean.ComFile;
import com.qixin.api.bean.ComLicenseFile;
import com.qixin.api.bean.FileResult;
import com.qixin.api.mapper.ComLicenseFileMapper;
import com.qixin.api.service.ComLicenseFileService;
import com.qixin.util.file.upload.UploadFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 公用营业执照文件表 服务实现类
 * </p>
 *
 * @author 闫亚茹
 * @since 2018-10-29
 */
@Service
public class ComLicenseFileServiceImpl extends ServiceImpl<ComLicenseFileMapper, ComLicenseFile> implements ComLicenseFileService {
    @Override
    public FileResult upload(MultipartFile file, ComLicenseFile comLicenseFile) {
        String url="";
        try {
            url=  UploadFile.fileUpload(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        comLicenseFile.setFileName(file.getOriginalFilename());
        comLicenseFile.setUrl(url);
        comLicenseFile.setCreateTime(new Date().getTime());
        FileResult result=new FileResult();
        //上传成功 返回数据 返回本次上传的文件ID
        if(this.insert(comLicenseFile)){
            result.setCode(0);
            result.setMsg("上传成功");
            Map map=new HashMap<>();
            map.put("fileId",comLicenseFile.getId());
            map.put("src",url);
            result.setData(map);
            return  result;
        };
        result.setCode(1);
        result.setMsg("上传失败");
        return  result;
    }


    //查询出来comFile表中relation_id是空的
    @Override
    public List<ComLicenseFile> selectnull() {
        return baseMapper.selectnull();
    }

    //删除
    @Override
    public boolean deleteRelationId() {
        return baseMapper.deleteRelationId();
    }

}
